package com.qyhl.webtv.module_news.luckydraw.fragment;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.LuckDrawInfoBean;
import com.qyhl.webtv.commonlib.entity.news.LuckDrawLiveBean;
import com.qyhl.webtv.commonlib.entity.news.LuckDrawPhaseBean;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.util.List;

/**
 * Created by Administrator on 2018/6/8.
 */

public class LuckDrawFragmentModel implements LuckDrawFragmentContract.LuckDrawModel {
    private LuckDrawFragmentPresenter mPresenter;

    public LuckDrawFragmentModel(LuckDrawFragmentPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 轮播
     *
     * @param sections
     */
    @Override
    public void getTop(String sections) {

        EasyHttp.get(NewsUrl.GET_TOWN_TOP)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("sections", sections)
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setTop(false, null);
                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        if (newsBeans!= null && newsBeans.size() > 0) {
                            mPresenter.setTop(true, newsBeans);
                        } else {
                            mPresenter.setTop(false, null);
                        }
                    }
                });

    }

    /**
     * 直播栏目
     *
     * @param catalogId
     */
    @Override
    public void getLive(String catalogId, String type) {
        EasyHttp.get(NewsUrl.GET_MENU_LIST)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("menuId", catalogId)
                .params("menuSecType", type)
                .params("menuType", "2")
                .execute(new SimpleCallBack<List<LuckDrawLiveBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setLive(null);
                    }

                    @Override
                    public void onSuccess(List<LuckDrawLiveBean> luckDrawLiveBeans) {
                        if (luckDrawLiveBeans != null && luckDrawLiveBeans.size() > 0) {
                            mPresenter.setLive(luckDrawLiveBeans);
                        } else {
                            mPresenter.setLive(null);
                        }
                    }
                });

    }

    /**
     * 获取开票有喜期号
     */
    @Override
    public void getPhase() {
        EasyHttp.get(NewsUrl.LUCK_DRAW_PHASE)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<LuckDrawPhaseBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setPhaseError(1, "获取信息失败！");
                    }

                    @Override
                    public void onSuccess(LuckDrawPhaseBean luckDrawPhaseBean) {
                        if (luckDrawPhaseBean != null) {
                            mPresenter.setPhase(luckDrawPhaseBean);
                        } else {
                            mPresenter.setPhaseError(1, "获取信息失败！");
                        }
                    }
                });
    }

    /**
     * 查询中奖信息
     *
     * @param phoneNum 手机号
     */
    @Override
    public void queryInfo(String phoneNum) {

        EasyHttp.get(NewsUrl.QUERY_LUCK_DRAW_INFO)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("phoneNum", phoneNum)
                .params("lastId", "0")
                .execute(new SimpleCallBack<List<LuckDrawInfoBean>>() {

                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202 ) {
                            mPresenter.setQueryInfoSuccess(false, null);
                        } else {
                            mPresenter.setQueryInfoError(1, "查询失败，请稍后再试！");
                        }
                    }

                    @Override
                    public void onSuccess(List<LuckDrawInfoBean> luckDrawInfoBeans) {
                        if ( luckDrawInfoBeans.size() > 0) {
                            mPresenter.setQueryInfoSuccess(true, luckDrawInfoBeans);
                        }else{
                            mPresenter.setQueryInfoSuccess(false, null);
                        }
                    }
                });
    }

    /**
     * 查询发票信息
     *
     * @param phoneNum 手机号
     */
    @Override
    public void queryTicketInfo(String phoneNum) {
        
        EasyHttp.get(NewsUrl.QUERY_LUCK_DRAW_TICKETS)
                .params("siteId",CommonUtils.getInstance().getSite()+"")
                .params("phoneNum",phoneNum)
                .params("lastId","0")
                .execute(new SimpleCallBack<List<LuckDrawInfoBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202 ) {
                            mPresenter.queryTicketInfoSuccess(null);
                        } else {
                            mPresenter.queryTicketInfoError(1, "查询失败，请稍后再试！");
                        }
                    }

                    @Override
                    public void onSuccess(List<LuckDrawInfoBean> luckDrawInfoBeans) {
                        if (luckDrawInfoBeans.size() > 0) {
                            mPresenter.queryTicketInfoSuccess(luckDrawInfoBeans);
                        } else  {
                            mPresenter.queryTicketInfoSuccess(null);
                        }
                    }
                });
        
    }

    /**
     * 提交信息
     *
     * @param code     发票代码
     * @param codeNum  发票号码
     * @param sum      发票金额
     * @param date     发票日期
     * @param phoneNum 手机号码
     */
    @Override
    public void commitInfo(String code, String codeNum, String sum, String date, String phoneNum, String businessName) {
        EasyHttp.post(NewsUrl.LUCK_DRAW_ADD)
                .params("siteId", CommonUtils.getInstance().getSite()+ "")
                .params("code", code)
                .params("codeNum", codeNum)
                .params("money", sum)
                .params("receiptDate", date)
                .params("phoneNum", phoneNum)
                .params("businessName", businessName)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.commentResult(false, "网络异常，提交失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.commentResult(true, "提交成功！");
                        } else {
                            mPresenter.commentResult(false, apiResult.getMessage() + "");
                        }
                    }
                });
    }

}
